/****************************************************************************

Force Engine v0.5

Creado: 28/03/08
Clase: MyScene.h
Hecho by: German Battiston AKA Melkor

****************************************************************************/

//---------------------------------------------------------------------------
#include "MyScene.h"
//---------------------------------------------------------------------------
MyScene::MyScene(Graphics &rkGraphics)
:
Scene(rkGraphics),
m_pkMesh(NULL),
m_pkMesh2(NULL),
m_pkLight(NULL),
m_bIsCW(false),
m_bIsCCW(false),
m_bIsCNone(true),
m_bIsWiref(false),
m_bLightOn(true),
m_pkGraphics(&rkGraphics),
Root(NULL),
Dragon1(NULL),
Dragon2(NULL),
m_pkSceneGraph(NULL),
m_pkSceneGraph2(NULL)
{
	rkGraphics.SetRenderStateCNONE();
}
//---------------------------------------------------------------------------
bool MyScene::OnInit()
{
	int iIndexLight = 0;

	m_pkMesh = new Mesh(* m_pkGraphics);
	m_pkMesh2 = new Mesh(* m_pkGraphics);
	
	if(!m_pkMesh->LoadMesh("Dragon.X"))
	{
		return false;
	}

	if(!m_pkMesh2->LoadMesh("Dragon.X"))
	{
		return false;
	}
	
	m_pkMesh->Initialize(* m_pkGraphics);
	m_pkMesh2->Initialize(* m_pkGraphics);

	m_pkSceneGraph = new SceneGraph(* m_pkGraphics);
	m_pkSceneGraph2 = new SceneGraph(* m_pkGraphics);
	Root = new Node(* m_pkGraphics);
	Dragon1 = new LeafNode(* m_pkGraphics);
	Dragon2 = new LeafNode(* m_pkGraphics);

	Root->Initialize("Root", NULL);
	Dragon1->Initialize(m_pkMesh,"Dragon1", Root);
	Dragon2->Initialize(m_pkMesh2,"Dragon2", Root);
	Dragon2->TranslateX(300.0f);
	
	m_pkSceneGraph->AttachNode(Root);
	m_pkSceneGraph->AttachNode(Dragon1);
	m_pkSceneGraph->AttachNode(Dragon2);

	Node * tmp = new Node(*m_pkGraphics);
	m_pkSceneGraph2->AttachNode(tmp);
	tmp = NULL;
	
	if(!m_pkSceneGraph2->LoadHierarchy("airplane 2.x"))
	{
		printf("LOADHIERARCHY FAIL");
	}

	m_pkSceneGraph2->GetRootNode()->Scale(D3DXVECTOR3(50,50,50));
	m_pkSceneGraph2->GetRootNode()->TranslateX(-200);

	D3DXVECTOR3 vPosition(0,0,-30.0f);
	D3DXVECTOR3 vDirection(0,0,0);

	iIndexLight = m_pkGraphics->CreateLight( D3DLIGHT_POINT,
											 0x00FFFFFF,
											 0x00FFFFFF,
											 0x00FFFFFF,
											 vPosition,
											 vDirection,
											 1000.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f );
	
	/*
	D3DXVECTOR3 vPosition(0, 0, 0);
	D3DXVECTOR3 vDirection(0, 0, 15.0f);

	iIndexLight = m_pkGraphics->CreateLight( D3DLIGHT_DIRECTIONAL,
											 0x00FFFFFF,
											 0x00FFFFFF,
											 0x00FFFFFF,
											 vPosition,
											 vDirection,
											 30.0f, 1.0f, 2.0f, 3.0f, 0.0f, 0.0f );*/
	
	/*
	D3DXVECTOR3 vPosition(10.0f, 10.0f, 10.0f);
	D3DXVECTOR3 vDirection(0, 0, 0);
	
	iIndexLight = m_pkGraphics->CreateLight( D3DLIGHT_SPOT,
											 0x00FFFFFF,
											 0x00FFFFFF,
											 0x00FFFFFF,
											 vPosition,
											 vDirection,
											 100.0f, 0.0f, 1.0f, 0.0f, 40.0f, 2.8f );*/
	

	m_pkGraphics->SetLight(m_pkGraphics->GetLight(iIndexLight));

	m_pkGraphics->EnableLight(0);
	
	return true;
}
//---------------------------------------------------------------------------
bool MyScene::OnUpdate(float fTimeBetweenFrames)
{
	if(m_pkInput->GetMouseClick(MB_DEFBUTTON1))
	{
		m_bIsWiref = !m_bIsWiref;		
	}

	if(m_pkInput->GetKeyDown(DIK_F))
	{
		m_bLightOn = !m_bLightOn;
	}


	if(m_pkInput->GetKeyDown(DIK_N))
	{
		m_bIsCNone = true;
		m_bIsCW = false;
		m_bIsCCW = false;
	}
	else if(m_pkInput->GetKeyDown(DIK_C))
	{
		m_bIsCW = true;
		m_bIsCCW = false;
		m_bIsCNone = false;
	}
	else if(m_pkInput->GetKeyDown(DIK_V))
	{
		m_bIsCCW = true;
		m_bIsCW = false;
		m_bIsCNone = false;
	}

	if(m_pkInput->GetKeyDown(DIK_D))
	{
		m_pkCamera->Strafe(0.5f);
	}
	
	if(m_pkInput->GetKeyDown(DIK_A))
	{
		m_pkCamera->Strafe(-0.5f);
	}

	if(m_pkInput->GetKeyDown(DIK_W))
	{
		m_pkCamera->Walk(0.5f);
	}

	if(m_pkInput->GetKeyDown(DIK_S))
	{
		m_pkCamera->Walk(-0.5f);
	}

	if(m_pkInput->GetKeyDown(DIK_SPACE))
	{
		m_pkCamera->Fly(0.5f);
	}

	if(m_pkInput->GetKeyDown(DIK_LCONTROL))
	{
		m_pkCamera->Fly(-0.5f);
	}
	
	// Movimiento del Nodo Root //--------------------------------------------------
	if(m_pkInput->GetKeyDown(DIK_L))
	{
		Root->TranslateX(0.5f);
	}
	else if(m_pkInput->GetKeyDown(DIK_J))
	{
		Root->TranslateX(-0.5f);
	}
	if(m_pkInput->GetKeyDown(DIK_I))
	{
		Root->TranslateZ(0.5f);
	}
	else if(m_pkInput->GetKeyDown(DIK_K))
	{
		Root->TranslateZ(-0.5f);
	}	
	// Movimiento del Nodo 1 //-------------------------------------------------
	if(m_pkInput->GetKeyDown(DIK_RIGHT))
	{
		Dragon1->TranslateX(0.5f);
	}
	else if(m_pkInput->GetKeyDown(DIK_LEFT))
	{
		Dragon1->TranslateX(-0.5f);
	}
	if(m_pkInput->GetKeyDown(DIK_UP))
	{
		Dragon1->TranslateZ(0.5f);
	}
	else if(m_pkInput->GetKeyDown(DIK_DOWN))
	{
		Dragon1->TranslateZ(-0.5f);
	}	
	// Movimiento del Nodo 2 //-------------------------------------------------
	if(m_pkInput->GetKeyDown(DIK_NUMPAD6))
	{
		Dragon2->TranslateX(0.5f);
	}
	else if(m_pkInput->GetKeyDown(DIK_NUMPAD4))
	{
		Dragon2->TranslateX(-0.5f);
	}
	if(m_pkInput->GetKeyDown(DIK_NUMPAD8))
	{
		Dragon2->TranslateZ(0.5f);
	}
	else if(m_pkInput->GetKeyDown(DIK_NUMPAD2))
	{
		Dragon2->TranslateZ(-0.5f);
	}
	// Update de camara //------------------------------------------------------

	m_pkSceneGraph->Update();
	m_pkSceneGraph2->Update();

	m_pkCamera->Yaw((float)m_pkInput->GetMouseRelPosX() * 0.007f);
	m_pkCamera->Pitch((float)m_pkInput->GetMouseRelPosY() * 0.007f);
	
	return true;
}
//---------------------------------------------------------------------------
void MyScene::OnDraw(Graphics &rkGraphics) const
{
	m_pkSceneGraph->Draw();
	m_pkSceneGraph2->Draw();

	if(m_bLightOn)
	{
		rkGraphics.SetRenderStateLightON();
	}
	else
	{
		rkGraphics.SetRenderStateLightOFF();
	}

	if(m_bIsWiref)
	{
		rkGraphics.SetRenderStateWire();
	}
	else
	{
		rkGraphics.SetRenderStateSolid();
	}

	if(m_bIsCW && !m_bIsCCW && !m_bIsCNone)
	{
		rkGraphics.SetRenderStateCW();
	}

	if(m_bIsCCW && !m_bIsCW && !m_bIsCNone)
	{
		rkGraphics.SetRenderStateCCW();
	}

	if(m_bIsCNone && !m_bIsCCW && !m_bIsCW)
	{
		rkGraphics.SetRenderStateCNONE();
	}
}
//---------------------------------------------------------------------------
bool MyScene::OnDeInit()
{
	m_pkMesh->DeInitialize();
	delete m_pkMesh;
	m_pkMesh = NULL;

	m_pkMesh2->DeInitialize();
	delete m_pkMesh2;
	m_pkMesh2 = NULL;
	
	delete m_pkSceneGraph;
	m_pkSceneGraph = NULL;

	return true;
}
//---------------------------------------------------------------------------
MyScene::~MyScene()
{
	
}
//---------------------------------------------------------------------------